home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / fopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  2.8 KB  |  137 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7.  
  8. #define _NFILE NFILES
  9.  
  10. void _getbuf(FILE *);
  11.  
  12. static FILE *_fopen(filename, mode, fp)
  13. _CONST char *filename;
  14. _CONST register char *mode;
  15. register FILE *fp;
  16. /*
  17.  *    INTERNAL FUNCTION.  Attempt to open <filename> in the given
  18.  *    <mode> and attach it to the stream <fp>
  19.  */
  20. {
  21.     register int h, i, iomode = 0, f = 0;
  22.     
  23.     while(*mode)
  24.     {
  25.     switch(*mode++)
  26.     {
  27.       case 'r':
  28.         f |= _IOREAD;
  29.         break;
  30.       case 'w':
  31.         f |= _IOWRT;
  32.         iomode |= (O_CREAT | O_TRUNC);
  33.         break;
  34.       case 'a':
  35.         f |= _IOWRT;
  36.         iomode |= (O_CREAT | O_APPEND);
  37.         break;
  38.       case '+':
  39.         f &= ~(_IOREAD | _IOWRT);
  40.         f |= _IORW;
  41.         break;
  42.       case 'b':
  43.         break;
  44.       case 't':
  45.         break;
  46.       default:
  47. /*        fputs("Illegal file mode.\n", stderr); */
  48.         return(NULL);
  49.     }
  50.     }
  51.     if((i = (f & (_IORW | _IOREAD | _IOWRT))) == 0)
  52.     return(NULL);
  53.     else if(i == _IOREAD)
  54.     iomode |= O_RDONLY;
  55.     else if(i == _IOWRT)
  56.     iomode |= O_WRONLY;
  57.     else
  58.     iomode |= O_RDWR;
  59.     h = open(filename, iomode, 0666);
  60.     if(h < 0)
  61.     {
  62.     return(NULL);        /* file open/create error */
  63.     }
  64.     if(isatty(h))
  65.     f |= (_IODEV | _IONBF);
  66.     else
  67.     f |= _IOFBF;
  68.     fp->_file = h;            /* file handle */
  69.     fp->_flag = f;            /* file status flags */
  70.     
  71.     return(fp);
  72. }
  73.  
  74. FILE *fopen(filename, mode)
  75. _CONST char *filename, *mode;
  76. {
  77.     register int i;
  78.     register FILE *fp = NULL;
  79.     
  80.     for(i=0; (!fp && (i < _NFILE)); ++i)
  81.     if(!(_iob[i]._flag & (_IORW | _IOREAD | _IOWRT))) 
  82.         fp = &_iob[i]; /* empty slot */
  83.     if(fp != NULL)
  84.     {
  85.     if(_fopen(filename, mode, fp) == NULL)
  86.         return NULL;
  87.     _getbuf(fp);
  88.     return fp;
  89.     }
  90.     else
  91.     return NULL;
  92. }
  93.  
  94. /*
  95.  * re-coded, 
  96.  *
  97.  *    ++jrb
  98.  */
  99. FILE *freopen(filename, mode, fp)
  100. _CONST char *filename, *mode;
  101. FILE *fp;
  102. {
  103.     unsigned int f;
  104.     
  105.     if(fp == NULL) return NULL;
  106.     
  107.     f = fp->_flag;
  108.     if((f & (_IORW | _IOREAD | _IOWRT)) == 0) return NULL; /* file not open */
  109.     if(fflush(fp) != 0) return NULL;                /* flush err */
  110.     if(close(fp->_file) != 0) return NULL;        /* close err */
  111.     
  112.     /* save buffer discipline and setbuf settings, and _IOWRT just for
  113.        determinining line buffering after the next _fopen */
  114.     f &= (_IOFBF | _IOLBF | _IONBF | _IOMYBUF | _IOWRT);
  115.     
  116.     /* open the new file according to mode */
  117.     if((fp = _fopen(filename, mode, fp)) == NULL)
  118.     return NULL;
  119.     if(fp->_flag & _IODEV)
  120.     { /* we are re-opening to a tty */
  121.     if((f & _IOFBF) && (f & _IOWRT) && (f & _IOMYBUF))
  122.     {   /* was a FBF & WRT & !setvbuff'ed  */
  123.         /* reset to line buffering */
  124.         f &= ~_IOFBF;
  125.         f |=  _IOLBF;
  126.     }
  127.     }
  128.     f &= ~_IOWRT; /* get rid of saved _IOWRT flag */
  129.     
  130.     /* put buffering and discipline flags in new fp->_flag */
  131.     fp->_flag &= ~(_IONBF | _IOLBF | _IOFBF | _IOMYBUF);
  132.     fp->_flag |= f;
  133.     
  134.     return fp;
  135.     
  136. }
  137.